package com.jthinker.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by chen.gang on 15-2-23.
 */
public class WebUtils
{
    public static Integer getInt(HttpServletRequest request, String key, Integer... defaultValues)
    {
        Integer v = null;
        String raw = getString(request, key);

        try
        {
            if (StringUtils.isNotBlank(raw))
            {
                v = Integer.parseInt(raw);
            }
            else
            {
                v = (defaultValues.length > 0) ? defaultValues[0] : null;
            }
        }
        catch (Exception e)
        {
            v = (defaultValues.length > 0) ? defaultValues[0] : null;
        }

        return v;
    }

    public static String getString(HttpServletRequest request, String key, String... defaultValues)
    {
        String v = request.getParameter(key);
        return (v != null) ? v : ((defaultValues.length > 0) ? defaultValues[0] : null);
    }
}
